package com.feicheng.lanniao.service.impl;

import com.feicheng.lanniao.entity.HouseLease;
import com.feicheng.lanniao.entity.HouseLeaseClientVo;
import com.feicheng.lanniao.entity.HouseRentClientVo;
import com.feicheng.lanniao.entity.HouseRentInfo;
import com.feicheng.lanniao.entity.HouseRentOwner;
import com.feicheng.lanniao.exception.SystemException;
import com.feicheng.lanniao.mapper.HouseRentClientVoMapper;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.IHouseRentClientVoService;
import com.feicheng.lanniao.service.IHouseRentInfoService;
import com.feicheng.lanniao.service.IHouseRentOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-07
 */
@Service
public class HouseRentClientVoServiceImpl extends ServiceImpl<HouseRentClientVoMapper, HouseRentClientVo> implements IHouseRentClientVoService {
	@Autowired
	private IHouseRentInfoService iHouseRentInfoService;
	@Autowired
	private IHouseRentOwnerService iHouseRentOwnerService;
	@Override
	public ResultInfo getRentClientPage(
			String title, 
			String detail,
			Integer  clientId , Integer currentPage, Integer pageSize) {
		QueryWrapper<HouseRentClientVo> hQueryWrapper = new QueryWrapper<HouseRentClientVo>();
		if(StringUtils.hasLength(title)) {
			hQueryWrapper.like("title",title);		
		}
		if (StringUtils.hasLength(detail)) {
			hQueryWrapper.like("detail", detail);
		}
		if (null != clientId) {
			hQueryWrapper.eq("client_id", clientId);
		}
		Page<HouseRentClientVo> pageInfo = new Page(currentPage,pageSize);
		return ResultInfo.success(this.page(pageInfo, hQueryWrapper));
	}

	@Override
	public ResultInfo findDetails(Integer houseRentId) {
		System.out.println(this.getById(houseRentId));
		return ResultInfo.success(this.getById(houseRentId));
	}

	@Override
	@Transactional(rollbackForClassName={"Exception"})
	public ResultInfo addRentClient(String tel,String wxNumber,String title, String detail, 
			Integer clientId) {
		if(null ==clientId || clientId==0) {
			throw new SystemException("clientId不能为空");
		}
		if (null == tel || tel.length() != 11) {
			throw new SystemException("请输入11位手机号");
		}
		if (null == wxNumber) {
			throw new SystemException("请输入微信号");
		}
		HouseRentInfo houseRentInfo = new HouseRentInfo();
		houseRentInfo.setTitle(title);
		houseRentInfo.setDetail(detail);
		houseRentInfo.setTel(tel);
		houseRentInfo.setWxNumber(wxNumber);
		iHouseRentInfoService.save(houseRentInfo);
		HouseRentOwner houseRentOwner = new HouseRentOwner();
		houseRentOwner.setHouseRentId(houseRentInfo.getHouseRentId());
		houseRentOwner.setClientId(clientId);
		iHouseRentOwnerService.save(houseRentOwner);
		return ResultInfo.success();
	}

	@Override
	public ResultInfo updateRentClient(Integer houseRentId, String title, String detail,
			String tel, String wxNumber, Integer clientId) {
		HouseRentInfo houseRentInfo = iHouseRentInfoService.getById(houseRentId);
		houseRentInfo.setTitle(title);
		houseRentInfo.setDetail(detail);
		houseRentInfo.setTel(tel);
		houseRentInfo.setWxNumber(wxNumber);
		return ResultInfo.success(iHouseRentInfoService.updateById(houseRentInfo));
	}
	@Override
	public ResultInfo deleteRentClient(HouseRentInfo houseRentInfo) {
		
		// 删除位
		houseRentInfo.setDelFlg(false);;
		// 更新
		iHouseRentInfoService.updateById(houseRentInfo);		
		return ResultInfo.success();		
	}

	@Override
	public ResultInfo deleteHouseLeaseClient(HouseRentInfo houseRentInfo) {
		// 删除位
		 houseRentInfo.setDelFlg(false);;
		// 更新
		iHouseRentInfoService.updateById( houseRentInfo);				
		return ResultInfo.success();
	}

}
